//
//  StatusRetweetedView.swift
//  微博个人swift
//
//  Created by 李灏 on 16/2/22.
//  Copyright © 2016年 lh. All rights reserved.
//

import UIKit
import SnapKit

class StatusRetweetedView: UIView {
    //添加约束对象属性 做记录使用
    var bottomConstaint: Constraint?

    //数据模型
    var statusRetweeted:Status? {
        didSet {
            retweetedLabel.text = statusRetweeted?.text
            pictureView.imageURLs = statusRetweeted?.imageURLs
            //卸载老的约束对象
            self.bottomConstaint?.uninstall()
            //根据数据判断要加哪个约束
            if let URLs = statusRetweeted?.imageURLs where URLs.count != 0 {
                //转发微博有配图
                self.snp_makeConstraints { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                }
            }else{
                //转发微博无配图
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(retweetedLabel)
        addSubview(pictureView)
        
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(retweetedLabel.snp_left)
        }
        //转发微博的底部需要添加约束
        self.snp_makeConstraints { (make) -> Void in
           self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    //懒加载视图
    private lazy var retweetedLabel:UILabel = UILabel(text: "哈哈", fontSize: 14, textColor: UIColor.lightGrayColor(), alignment: .Left)
    
    //图片视图
    private lazy var pictureView:StatusCellPictureView = StatusCellPictureView()
}
